using System;
using System.Web.UI;
using CodeStory.Core.ObjectEntity;
using CodeStory.Core.UI;
using CodeStory.Core;

namespace CodeStory.WebApplication.Modules.Core.Admin
{
	[NavigationPortlet("CodeStory_Core_Admin_CreateLayout")]
	public partial class CreateLayout : Portlet
	{
		protected override void OnInit(EventArgs e) {
			Load += new EventHandler(CreateLayout_Load);
			btnCreate.Click += new EventHandler(ButtonCreate_Click);
			base.OnInit(e);
		}

		private void ButtonCreate_Click(object sender, EventArgs e) {
			//throw new Exception("The method or operation is not implemented.");
			string layoutid = Request.QueryString["layoutid"];
			LayoutObject obj = null;
			if (string.IsNullOrEmpty(layoutid)) {
				obj = LayoutObject.New();
			} else {
				obj = LayoutObject.Read(new Guid(layoutid));
			}
			obj.LayoutName = txtLayoutName.Text;
			obj.FriendlyLayoutName = txtFriendlyLayoutName.Text;
			obj.LayouTemplate = txtLayoutTemplate.Text;
			obj.ImageFile = txtImageFile.Text;
			obj.Save();
		}

		private void CreateLayout_Load(object sender, EventArgs e) {
			//throw new Exception("The method or operation is not implemented.");
			if (!IsPostBack) {
				TryBindLayoutObject();
			}
		}

		private void TryBindLayoutObject() {
			string layoutid = Request.QueryString["layoutid"];
			if (string.IsNullOrEmpty(layoutid)) {
				return;
			}
			LayoutObject obj = LayoutObject.Read(new Guid(layoutid));
			txtLayoutName.Text = obj.LayoutName;
			txtFriendlyLayoutName.Text = obj.FriendlyLayoutName;
			txtLayoutTemplate.Text = obj.LayouTemplate;
			txtImageFile.Text = obj.ImageFile;
		}
	}
}